package com.gzucm.Scholarship.service;

import com.gzucm.Scholarship.common.ConstValues;
import com.gzucm.Scholarship.common.base.BaseServiceImpl;
import com.gzucm.Scholarship.common.enums.SystemKind;
import com.gzucm.Scholarship.common.enums.SystemState;
import com.gzucm.Scholarship.dao.SystemValueDao;
import com.gzucm.Scholarship.dto.ResultDTO;
import com.gzucm.Scholarship.dto.SystemStateDTO;
import com.gzucm.Scholarship.dto.TaskDTO;
import com.gzucm.Scholarship.dto.TaskStateDTO;
import com.gzucm.Scholarship.entity.SystemValue;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SystemService extends BaseServiceImpl<SystemValue> {

    private SystemValueDao dao;

    private RedisService redisService;

    public SystemService(SystemValueDao dao, RedisService redisService) {
        super(dao);
        this.dao = dao;
        this.redisService = redisService;
    }

    public List<SystemValue> findByKind(SystemKind kind) {
        return dao.findByKind(kind.getKind());
    }

    public SystemValue findByKindAndCode(SystemKind kind, SystemState state) {
        return dao.findByKindAndCode(kind.getKind(), state.name());
    }

    public Integer deleteByKindAndCode(SystemKind kind, SystemState state) {
        return dao.deleteByKindAndCode(kind.getKind(), state.name());
    }

    public Integer deleteByKind(SystemKind kind) {
        return dao.deleteByKind(kind.getKind());
    }

    @Transactional
    public ResultDTO<List<SystemValue>> changeState(TaskDTO taskDTO) {
        if (taskDTO.getStateList().size() > 4) {
            return ResultDTO.failure(HttpStatus.NOT_ACCEPTABLE, "定时队列超出数量");
        }
        int t1 = 0, t2 = 0, t3 = 0, t4 = 0;
        SystemState state;
        SystemValue value;
        List<SystemValue> valueList = new ArrayList<>();
        for (TaskStateDTO stateDTO : taskDTO.getStateList()) {
            state = SystemState.get(stateDTO.getStateName());
            switch (state) {
                case OPEN_UP:
                    t1++;
                    break;
                case EXAMINE:
                    t2++;
                    break;
                case REPORT:
                    t3++;
                    break;
                case SUSPEND:
                    t4++;
                    break;
            }
            if (t1 > 1 || t2 > 1 || t3 > 1 || t4 > 1) {
                return ResultDTO.failure(HttpStatus.NOT_ACCEPTABLE, "存在重复的状态");
            }
            value = new SystemValue();
            value.setKind(SystemKind.SYSTEM_STATE_TIMER.getKind());
            value.setCode(state.name());
            value.setDateValue(stateDTO.getStateValue());
            value.setDetail(state.getDetail());
            value.setDeleteflag(false);
            valueList.add(value);
        }
        deleteByKind(SystemKind.SYSTEM_STATE_TIMER);
        return ResultDTO.success("设置成功", save(valueList));
    }

    public ResultDTO<SystemStateDTO> changeState(String val) {
        SystemState state = SystemState.get(val);
        if (Objects.isNull(state)) {
            return ResultDTO.failure(HttpStatus.BAD_REQUEST, "错误的系统代码参数");
        }
        redisService.set(ConstValues.REDIS_SYSTEM_STATE, state);
        SystemStateDTO dto = SystemStateDTO.builder().state(state).message(state.getDetail()).build();
        dto.initOpen();
        return ResultDTO.success("更改系统状态成功", dto);
    }

    public ResultDTO<SystemStateDTO> getState() {
        String obj = redisService.get(ConstValues.REDIS_SYSTEM_STATE, String.class);
        SystemState state = SystemState.get(obj);
        if (Objects.isNull(state)) {
            state = SystemState.SUSPEND;
            redisService.set(ConstValues.REDIS_SYSTEM_STATE, state);
        }
        SystemStateDTO dto = SystemStateDTO.builder().state(state).message(state.getDetail()).build();
        dto.initOpen();
        return ResultDTO.success("获取成功", dto);
    }

    public ResultDTO<TaskDTO> getTaskList() {
        List<SystemValue> values = this.findByKind(SystemKind.SYSTEM_STATE_TIMER);
        List<TaskStateDTO> dtos = new ArrayList<>();
        TaskStateDTO dto;
        for (SystemValue value : values) {
            dto = new TaskStateDTO();
            dto.setStateName(value.getCode());
            dto.setStateValue(value.getDateValue());
            dtos.add(dto);
        }
        TaskDTO taskDTO = new TaskDTO();
        taskDTO.setStateList(dtos);
        return ResultDTO.success("获取成功", taskDTO);
    }
}
